<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: liunwcj
 * Date: 15-4-30
 * Time: 下午2:54
 */
class Message_model extends CI_Model
{
//  定义静态变量 表名称
    const TB_MES = 'message';

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function get_all_messages() {
        $query = $this->db->where('1=1')->get(self::TB_MES);
        $num_row = $query->num_rows();
        $result_row = $query->result_array();
        return array('num_row' => $num_row, 'result_row' => $result_row);
    }

    function get_message($_id) {
        $query = $this->db->where('id', $_id)->get(self::TB_MES);
        //数组形式
        return $query->result_array();
    }

    function get_item_message($num, $offset) {
        $this->db->order_by('id', 'desc');
        $query = $this->db->get(self::TB_MES, $num, $offset);
        //对象形式
        return $query->result();
    }

    function add_message($arrs) {
        $query = $this->db->insert(self::TB_MES, $arrs);
        return $this->db->affected_rows();
    }

    function edit_message($arrs) {
        $this->db->where('id', $arrs['id']);
        $this->db->update(self::TB_MES, $arrs);
    }

    function del_message($_id) {
        $this->db->where('id', $_id);
        $this->db->delete(self::TB_MES);
    }
}